package com.tj.jinzao.presenter;

import android.content.Context;

import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.model.User;

import java.util.List;
import java.util.Map;

/**
 * 用户信息
 * Created by wjy 2018/12/10.
 */
public class UserPresenter extends BasePresenter<IUploadView<List<User>>> {

    public UserPresenter(IUploadView<List<User>> view) {
        super(view);
    }

    //获取个人目标
    public void getBLList() {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getBLList(uId), new SubscriberCallBack<List<Target>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<Target> targets) {
                if (mView != null) mView.onCaseSuccess(targets);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onCaseFailed(response.getMessage());
            }
        });
    }


    public void updateInfo(Context context,Map<String, Object> map) {
        long id = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.updateUserInfo(id, map), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                if(user!=null){
                    UserManager.saveUserData(user);
                    updateUserInfo(user);
                }

                if (mView != null) mView.onRequestSuccess(null);
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    @Override
    protected void onUploadSuccess(String localPath, String response) {
        if (mView != null) mView.onUploadSuccess(localPath, response);
    }

    @Override
    protected void onUploadFailed(String localPath, ResultResponse response) {
        if (mView != null) mView.onRequestError(null);
    }
}